﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;

namespace FPSClient.model
{
    public class CappXmlParser
    {
        public static QMFawTechnicsInfo ParseQMFawTechnicsInfo(XmlNode node)
        {
            QMFawTechnicsInfo technicsInfo = null;
            if (node != null && node.Name.CompareTo("QMFawTechnicsInfo") == 0)
            {
                XmlElement xmlElement = (XmlElement)node;
                technicsInfo = new QMFawTechnicsInfo();
                Type t = technicsInfo.GetType();

                foreach (MemberInfo mi in t.GetMembers())
                {
                    if (mi.MemberType == MemberTypes.Field)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetField(mi.Name).SetValue(technicsInfo, value);
                    }
                    else if (mi.MemberType == MemberTypes.Property)
                    {
                        if (mi.Name == "steps")
                        {
                            XmlNode n = xmlElement.SelectSingleNode("steps");
                            if (n != null)
                            {
                                foreach (XmlNode childNode in n.ChildNodes)
                                {
                                    QMProcedureInfo procedureInfo = ParseQMProcedureInfo(childNode);
                                    if (procedureInfo != null)
                                    {
                                        technicsInfo.steps.Add(procedureInfo);
                                    }
                                }
                            }
                        }
                        else
                        {
                            string value = xmlElement.GetAttribute(mi.Name);
                            t.GetProperty(mi.Name).SetValue(technicsInfo, value);
                        }
                    }
                }
            }
            return technicsInfo;
        }

        public static QMProcedureInfo ParseQMProcedureInfo(XmlNode node)
        {
            QMProcedureInfo procedureInfo = null;
            if (node != null && node.Name.CompareTo("QMProcedureInfo") == 0)
            {
                XmlElement xmlElement = (XmlElement)node;
                procedureInfo = new QMProcedureInfo();
                Type t = procedureInfo.GetType();

                foreach (MemberInfo mi in t.GetMembers())
                {
                    if (mi.MemberType == MemberTypes.Field)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetField(mi.Name).SetValue(procedureInfo, value);
                    }
                    else if (mi.MemberType == MemberTypes.Property)
                    {
                        if (mi.Name == "paces")
                        {
                            XmlNode n = xmlElement.SelectSingleNode("paces");
                            if (n != null)
                            {
                                foreach (XmlNode childNode in n.ChildNodes)
                                {
                                    QMProcedureInfo info = ParseQMProcedureInfo(childNode);
                                    if (info != null)
                                    {
                                        procedureInfo.paces.Add(info);
                                    }
                                }
                            }
                        }
                        else if (mi.Name == "parts")
                        {
                            XmlNode n = xmlElement.SelectSingleNode("parts");
                            if (n != null)
                            {
                                foreach (XmlNode childNode in n.ChildNodes)
                                {
                                    part part = Parsepart(childNode);
                                    if (part != null)
                                    {
                                        procedureInfo.parts.Add(part);
                                    }
                                }
                            }
                        }
                        else if (mi.Name == "materials")
                        {
                            XmlNode n = xmlElement.SelectSingleNode("materials");
                            if (n != null)
                            {
                                foreach (XmlNode childNode in n.ChildNodes)
                                {
                                    material material = Parsematerial(childNode);
                                    if (material != null)
                                    {
                                        procedureInfo.materials.Add(material);
                                    }
                                }
                            }
                        }
                        else if (mi.Name == "tools")
                        {
                            XmlNode n = xmlElement.SelectSingleNode("tools");
                            if (n != null)
                            {
                                foreach (XmlNode childNode in n.ChildNodes)
                                {
                                    tool tool = Parsetool(childNode);
                                    if (tool != null)
                                    {
                                        procedureInfo.tools.Add(tool);
                                    }
                                }
                            }
                        }
                        else if (mi.Name == "equipments")
                        {
                            XmlNode n = xmlElement.SelectSingleNode("equipments");
                            if (n != null)
                            {
                                foreach (XmlNode childNode in n.ChildNodes)
                                {
                                    equipment equipment = Parseequipment(childNode);
                                    if (equipment != null)
                                    {
                                        procedureInfo.equipments.Add(equipment);
                                    }
                                }
                            }
                        }
                        else if (mi.Name == "zuoyemiaoshus")
                        {
                            XmlNode n = xmlElement.SelectSingleNode("zuoyemiaoshus");
                            if (n != null)
                            {
                                foreach (XmlNode childNode in n.ChildNodes)
                                {
                                    zuoyemiaoshu zuoyemiaoshu = Parsezuoyemiaoshu(childNode);
                                    if (zuoyemiaoshu != null)
                                    {
                                        procedureInfo.zuoyemiaoshus.Add(zuoyemiaoshu);
                                    }
                                }
                            }
                        }
                        else
                        {
                            string value = xmlElement.GetAttribute(mi.Name);
                            t.GetProperty(mi.Name).SetValue(procedureInfo, value);
                        }
                    }
                }
            }

            return procedureInfo;
        }

        public static part Parsepart(XmlNode node)
        {
            part part = null;
            if (node != null && node.Name.CompareTo("part") == 0)
            {
                XmlElement xmlElement = (XmlElement)node;
                part = new part();
                Type t = part.GetType();

                foreach (MemberInfo mi in t.GetMembers())
                {
                    if (mi.MemberType == MemberTypes.Field)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetField(mi.Name).SetValue(part, value);
                    }
                    else if (mi.MemberType == MemberTypes.Property)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetProperty(mi.Name).SetValue(part, value);
                    }
                }

            }
            return part;
        }
        public static material Parsematerial(XmlNode node)
        {
            material material = null;
            if (node != null && node.Name.CompareTo("material") == 0)
            {
                XmlElement xmlElement = (XmlElement)node;
                material = new material();
                Type t = material.GetType();

                foreach (MemberInfo mi in t.GetMembers())
                {
                    if (mi.MemberType == MemberTypes.Field)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetField(mi.Name).SetValue(material, value);
                    }
                    else if (mi.MemberType == MemberTypes.Property)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetProperty(mi.Name).SetValue(material, value);
                    }
                }
            }
            return material;
        }
        public static tool Parsetool(XmlNode node)
        {
            tool tool = null;
            if (node != null && node.Name.CompareTo("tool") == 0)
            {
                XmlElement xmlElement = (XmlElement)node;
                tool = new tool();
                Type t = tool.GetType();

                foreach (MemberInfo mi in t.GetMembers())
                {
                    if (mi.MemberType == MemberTypes.Field)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetField(mi.Name).SetValue(tool, value);
                    }
                    else if (mi.MemberType == MemberTypes.Property)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetProperty(mi.Name).SetValue(tool, value);
                    }
                }
            }
            return tool;
        }
        public static equipment Parseequipment(XmlNode node)
        {
            equipment equipment = null;
            if (node != null && node.Name.CompareTo("equipment") == 0)
            {
                XmlElement xmlElement = (XmlElement)node;
                equipment = new equipment();
                Type t = equipment.GetType();

                foreach (MemberInfo mi in t.GetMembers())
                {
                    if (mi.MemberType == MemberTypes.Field)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetField(mi.Name).SetValue(equipment, value);
                    }
                    else if (mi.MemberType == MemberTypes.Property)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetProperty(mi.Name).SetValue(equipment, value);
                    }
                }
            }
            return equipment;
        }

        public static zuoyemiaoshu Parsezuoyemiaoshu(XmlNode node)
        {
            zuoyemiaoshu zuoyemiaoshu = null;
            if (node != null && node.Name.CompareTo("zuoyemiaoshu") == 0)
            {
                XmlElement xmlElement = (XmlElement)node;
                zuoyemiaoshu = new zuoyemiaoshu();
                Type t = zuoyemiaoshu.GetType();
                XmlNode miaoshu = xmlElement.SelectSingleNode("miaoshu");
                XmlNode zhuyishixiang = xmlElement.SelectSingleNode("zhuyishixiang");


                foreach (MemberInfo mi in t.GetMembers())
                {
                    if (mi.MemberType == MemberTypes.Field)
                    {
                        string value = xmlElement.GetAttribute(mi.Name);
                        t.GetField(mi.Name).SetValue(zuoyemiaoshu, value);
                    }
                    else if (mi.MemberType == MemberTypes.Property)
                    {
                        if (mi.Name.CompareTo("miaoshu") == 0)
                        {
                            if (miaoshu != null)
                            {
                                XmlElement xmlItemElement = (XmlElement)miaoshu;
                                zuoyemiaoshu.miaoshu += xmlItemElement.InnerText;
                            }
                        }
                        else if (mi.Name.CompareTo("zhuyishixiang") == 0)
                        {
                            if (zhuyishixiang != null)
                            {
                                XmlElement xmlItemElement = (XmlElement)zhuyishixiang;
                                zuoyemiaoshu.zhuyishixiang += xmlItemElement.InnerText;
                            }
                        }
                    }
                }

            }
            return zuoyemiaoshu;
        }
    }
}
